package com.kfd.demo.controller;

import com.kfd.demo.entity.user.Address_Tb;
import com.kfd.demo.service.UserExpandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/* *
* 对用户功能进行拓展的Controller
* 功能包括：
*  1.个人地址获取
*  2.商品详情获取
*
*  @Controller 为UserExpandController添加一个Controller标识
*  @ResponseBody 设置该类所有的方法都是返回数据的
*  @CrossOrigin(origins = "*") 设置允许来自任何资源的访问
* */
@Controller
@ResponseBody
@CrossOrigin(origins = "*")
public class UserExpandController {

    @Autowired
    UserExpandService userExpandService;

    // 根据商品名字查询商品
    @PostMapping(value = "/queryCommoditys", produces = "application/json;charset=UTF-8")
    public List queryCommoditys(@RequestParam("commodityName") String commodityName){
        System.out.println("========= queryCommoditys =========");
        System.out.println("访问了该方法：" + commodityName);

        List list = new ArrayList<>();

        List queryData = userExpandService.queryCommodity(commodityName);
        Map<String, Object> map = new HashMap<>();
        if(commodityName.length() <= 0){
            queryData = null;
            map.put("data", queryData);
            map.put("status", 405);
            map.put("msg", "商品名不能为空！");
            list.add(map);
            return list;
        }

        if(queryData.size() > 0){
            // 有数据的情况下
            map.put("data", queryData);
            map.put("status", 200);
        } else {
            // 没有数据的情况下
            map.put("data", queryData);
            map.put("status", 400);
        }

        list.add(map);
        return list;
    }

    // 商品详情获取
    @PostMapping(value = "/getProductDetailsList", produces = "application/json;charset=UTF-8")
    public List getProductDetailsList(@RequestParam("right_menu_data_id") int rightMenuDataId){
        System.out.println("========= 这里是getProductDetails方法 =========");
        System.out.println("访问了该方法：" + rightMenuDataId);
        List list = new ArrayList();
        List thaliContain = userExpandService.getProductDetails(rightMenuDataId);
        Map<String, Object> map = new HashMap<>();

        if(thaliContain.size() > 0){
            // 有数据的情况下
            map.put("data", thaliContain);
            map.put("status", 200);
        } else {
            // 没有数据的情况下
            map.put("data", thaliContain);
            map.put("status", 400);
        }

        list.add(map);
        return list;
    }

    // 获取用户所有的地址
    @PostMapping(value = "/getAddressList", produces = "application/json;charset=UTF-8")
    public List getAddressList(@RequestParam("u_id") int uId){
//        System.out.println("========= 这里是getAddressList方法 =========");
        List list = new ArrayList();
        List address = userExpandService.getAddress(uId);
        Map<String, Object> map = new HashMap<>();

        if(address.size() > 0){
            // 有数据的情况下
            map.put("data", address);
            map.put("status", 200);
        } else {
            // 没有数据的情况下
            map.put("data", address);
            map.put("status", 400);
        }
        list.add(map);
        return list;
    }

    // 添加地址(未完待续...)
    @PostMapping("/addAddress")
    public int addAddress(@ModelAttribute Address_Tb addressInfo){
        System.out.println("========= 这里是addAddress方法 =========");
        System.out.println(addressInfo);
        //
        return 0;
    }
}
